package com.oceansoft.mobile.biz.front.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oceansoft.mobile.biz.front.entity.Condition;
import com.oceansoft.mobile.biz.front.service.IQuestionService;
import com.oceansoft.mobile.common.util.Result;

@Controller
@RequestMapping("sc/question")
public class QuestionController {

	@Resource
    private IQuestionService questionService;

	/**
	 * 进入答题页面
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String gotoQuestion(HttpServletRequest request,Model model) throws ParseException {
		model.addAttribute("openId", request.getParameter("openId"));
		Condition condition = new Condition();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    
		   Date date1 =sd.parse("2015-09-21 10:00:00");
		   Date date2=sd.parse("2015-10-21 00:00:00");
		   String data = sd.format(new Date());
		   Date date3=sd.parse(data);
		   long number1=date1.getTime()-date3.getTime();
		   long number2=date2.getTime()-date3.getTime();

		   if( number1<0&number2>0){
			   condition.setFlag(1);
			   return "mobile/police/question";
			  
		   }else if(number2<0){
			   condition.setFlag(2);
			   return  "guagua/guagua2";
		   }else if(number1>0){
			   condition.setFlag(3);
			   return  "guagua/guagua1";
		   }

		   return "mobile/police/question";
	}
	
	/**
	 * 加载所有问题列表
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result questionList() {
    	return questionService.getQuestionList();
    }
    
	/**
	 * 提交回答
	 * JSON格式 [{"answer":"A","questionId":"1"},{"answer":"B","questionId":"2"}]
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public Result submitAnswer(HttpServletRequest request) {
    	String answer = request.getParameter("answer");
    	if(StringUtils.isEmpty(answer))
    		return new Result(Result.FAILURE,"答案不能为空");
    	
    	return questionService.submitAnswer(answer);
    }
    
    /**
     * 判断用户是否已经参加过此活动
     */
    @ResponseBody
    @RequestMapping(value = "isjoin", method = RequestMethod.GET)
    public Result isjoin(HttpServletRequest request) {
    	return questionService.isJoin(request.getParameter("openId"));
    }
}
